

#include "pwm.h"

/* PWM export */
int pwm_export(unsigned int pin)
{
	int fd;

	switch(pin){
	case 1:{
		fd = open(SYSFS_PWM_DIR "/pwmchip0/export", O_WRONLY);
		if (fd < 0) {
			dbg_info ("\nFailed export PWM_B\n");
			return -1;
		}

		write(fd, "0", 2);
		close(fd);
		return 0;
		break;

	}
	case 2:{
		fd = open(SYSFS_PWM_DIR "/pwmchip1/export", O_WRONLY);
		if (fd < 0) {
			dbg_info ("\nFailed export PWM_D\n");
			return -1;
		}

		write(fd, "0", 2);
		close(fd);
		return 0;
		break;
	}
	case 4:{
		fd = open(SYSFS_PWM_DIR "/pwmchip3/export", O_WRONLY);
		if (fd < 0) {
			dbg_info ("\nFailed export PWM_C\n");
			return -1;
		}

		write(fd, "0", 2);
		close(fd);
		return 0;
		break;
	}
	default:{
		dbg_info("wrong PWM channel input\n");
		return -1;
	}
	}

}

/* PWM unexport */
int pwm_unexport(unsigned int pin)
{
	int fd;

	switch(pin){
	case 1:{
		fd = open(SYSFS_PWM_DIR "/pwmchip0/export", O_WRONLY);
		if (fd < 0) {
			dbg_info ("\nFailed unexport PWM_B\n");
			return -1;
		}

		write(fd, "1", 2);
		close(fd);
		return 0;
        break;
	}
	case 2:{
		fd = open(SYSFS_PWM_DIR "/pwmchip1/export", O_WRONLY);
		if (fd < 0) {
			dbg_info ("\nFailed unexport PWM_D\n");
			return -1;
		}

		write(fd, "1", 2);
		close(fd);
		return 0;
		break;
	}
	case 4:{
		fd = open(SYSFS_PWM_DIR "/pwmchip3/export", O_WRONLY);
		if (fd < 0) {
			dbg_info ("\nFailed unexport PWM_C\n");
			return -1;
		}

		write(fd, "1", 2);
		close(fd);
		return 0;
		break;
	}
	default:{
		dbg_info("wrong PWM channel input\n");
		return -1;
	}
	}
}

/* PWM configuration */
int pwm_config(PWM *pwm)
{
	int fd,len_p,len_d;
	char buf_p[MAX_BUF];
	char buf_d[MAX_BUF];
	
	len_p =sprintf(buf_p, "%d", pwm->period);
	len_d =sprintf(buf_d, "%d", pwm->duty_cycle);  

	switch(pwm->pin){
	case 1:{
			/* set pwm period */
		    fd = open(SYSFS_PWM_DIR "/pwmchip0/pwm0/period", O_WRONLY);
			if (fd < 0) {
				dbg_info ("\nFailed set PWM_B period\n");
				return -1;
			}

			write(fd, buf_p, len_p);
			/* set pwm duty cycle */
			fd = open(SYSFS_PWM_DIR "/pwmchip0/pwm0/duty_cycle", O_WRONLY);
			if (fd < 0) {
				dbg_info ("\nFailed set PWM_B duty cycle\n");
				return -1;
			}

			write(fd, buf_d, len_d);

			close(fd);
			return 0;
			break;
		}
		case 2:{
			/* set pwm period */
			fd = open(SYSFS_PWM_DIR "/pwmchip1/pwm0/period", O_WRONLY);
			if (fd < 0) {
				dbg_info ("\nFailed set PWM_D period\n");
				return -1;
			}

			write(fd, buf_p, len_p);
			/* set pwm duty cycle */
			fd = open(SYSFS_PWM_DIR "/pwmchip1/pwm0/duty_cycle", O_WRONLY);
			if (fd < 0) {
				dbg_info ("\nFailed set PWM_D duty cycle\n");
				return -1;
			}

			write(fd, buf_d, len_d);

			close(fd);
			return 0;
			break;
		}
		case 4:{
			/* set pwm period */
			fd = open(SYSFS_PWM_DIR "/pwmchip3/pwm0/period", O_WRONLY);
			if (fd < 0) {
				dbg_info ("\nFailed set PWM_C period\n");
				return -1;
			}

			write(fd, buf_p, len_p);
			/* set pwm duty cycle */
			fd = open(SYSFS_PWM_DIR "/pwmchip3/pwm0/duty_cycle", O_WRONLY);
			if (fd < 0) {
				dbg_info ("\nFailed set PWM_C duty cycle\n");
				return -1;
			}

			write(fd, buf_d, len_d);

			close(fd);
			return 0;
			break;
		}
		default:{
			dbg_info("wrong PWM channel input\n");
			return -1;
		}
	}
}

/* PWM enable */
int pwm_enable(unsigned int pin)
{
	int fd;

	switch(pin){
	case 1:{
		    fd = open(SYSFS_PWM_DIR "/pwmchip0/pwm0/enable", O_WRONLY);
			if (fd < 0) {
				dbg_info ("\nFailed enable PWM_B\n");
				return -1;
			}
			write(fd, "1", 2);

			close(fd);
			return 0;
			break;
		}
		case 2:{
			fd = open(SYSFS_PWM_DIR "/pwmchip1/pwm0/enable", O_WRONLY);
			if (fd < 0) {
				dbg_info ("\nFailed enable PWM_D\n");
				return -1;
			}
			write(fd, "1", 2);

			close(fd);
			return 0;
			break;
			}
		case 4:{
			fd = open(SYSFS_PWM_DIR "/pwmchip3/pwm0/enable", O_WRONLY);
			if (fd < 0) {
				dbg_info ("\nFailed enable PWM_C\n");
				return -1;
			}
			write(fd, "1", 2);

			close(fd);
			return 0;
			break;
		}
		default:{
			dbg_info("wrong PWM channel input\n");
			return -1;
		}
	}
}

/* PWM disable */
int pwm_disable(unsigned int pin)
{
	int fd;

		switch(pin){
		case 1:{
			    fd = open(SYSFS_PWM_DIR "/pwmchip0/pwm0/enable", O_WRONLY);
				if (fd < 0) {
					dbg_info ("\nFailed disable PWM_B\n");
					return -1;
				}
				write(fd, "0", 2);

				close(fd);
				return 0;
				break;
			}
			case 2:{
				fd = open(SYSFS_PWM_DIR "/pwmchip1/pwm0/enable", O_WRONLY);
				if (fd < 0) {
					dbg_info ("\nFailed disable PWM_D\n");
					return -1;
				}
				write(fd, "0", 2);

				close(fd);
				return 0;
				break;
				}
			case 4:{
				fd = open(SYSFS_PWM_DIR "/pwmchip3/pwm0/enable", O_WRONLY);
				if (fd < 0) {
					dbg_info ("\nFailed disable PWM_C\n");
					return -1;
				}
				write(fd, "0", 2);

				close(fd);
				return 0;
				break;
			}
			default:{
				dbg_info("wrong PWM channel input\n");
				return -1;
			}
		}
}
